package com.xhj.myframe.myAQS;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

import static java.lang.System.currentTimeMillis;
/** @author haijianxia pure_xhj@163.com @Date 2021-08-25 @Time 15:24 */
public class LockSupportDemo {

  public static void main(String[] args) {
    /*  System.out.println("=============test01==============");
    	  System.out.println("");
    	  long time01 = currentTimeMillis();
    	  Thread aaa = new Thread(() -> {
    		  try {
    			  TimeUnit.MICROSECONDS.sleep(2500);
    		  } catch (InterruptedException e) {
    			  e.printStackTrace();
    		  }
    		  System.out.println(Thread.currentThread().getName() + " come in...");
    		  LockSupport.park();
    		  System.out.println(Thread.currentThread().getName() + " continue...");
    	  }, "AAA");
    	  aaa.start();

    	  new Thread(()->{
    		  try {TimeUnit.MICROSECONDS.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
    		  System.out.println(Thread.currentThread().getName()+" caller...");
    		  LockSupport.unpark(aaa);
    	  },"BBB").start();

    	  long time0101  = currentTimeMillis() - time01;
    	  System.out.println("\n耗时 =  " + time0101  + "ms");
    	  System.out.println("=================================");
    */

    //	  test01();

    try {
      TimeUnit.MICROSECONDS.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("\n- - - - - - - - - - - - - -");

    test02();
  }

  @Test
  public static void test01() {
    System.out.println("=============test01==============");
    System.out.println("");
    long time01 = currentTimeMillis();
    Thread aaa =
        new Thread(
            () -> {
              try {
                TimeUnit.MICROSECONDS.sleep(500);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName() + " come in...");
              LockSupport.park();
              System.out.println(Thread.currentThread().getName() + " continue...");
            },
            "AAA");
    aaa.start();

    new Thread(
            () -> {
              try {
                TimeUnit.MICROSECONDS.sleep(1500);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName() + " caller...");
              LockSupport.unpark(aaa);
            },
            "BBB")
        .start();

    long time0101 = currentTimeMillis() - time01;
    System.out.println("\n耗时 =  " + time0101 + "ms");
    System.out.println("=================================");
  }

  /** 两行park,unpark引起阻塞 */
  @Test
  public static void test02() {
    System.out.println("=============test02==============");
    System.out.println("两行park引起阻塞");
    long time02 = currentTimeMillis();

    Thread aaa =
        new Thread(
            () -> {
              try {
                TimeUnit.MICROSECONDS.sleep(2500);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName() + " caller...");
              LockSupport.park();
              LockSupport.park();
            },
            "AAA");
    aaa.start();
    new Thread(
            () -> {
              try {
                TimeUnit.MICROSECONDS.sleep(2000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName() + " caller...");
              LockSupport.unpark(aaa);
              LockSupport.unpark(aaa);
            },
            "BBB")
        .start();

    long time0202 = currentTimeMillis() - time02;
    System.out.println("\n耗时 =  " + time0202 + "ms");
    System.out.println("=================================");
  }
}
